/*------------------------------------------------------------------------------
 * PROJ : Prototype
 * NAME : sculove.spring.framework.util.SessionUtil.java
 *        
 * Copyright 2009 sculove All rights reserved
 *------------------------------------------------------------------------------
*/
package sculove.spring.framework.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <pre>
 * sculove.spring.framework.util.SessionUtil.java
 * 세션과 관련된 Util
 * 
 * </pre>
 * @date : 2009. 12. 4. 오전 10:40:54
 * @version :
 * @author : sculove
 */
public class SessionUtil {
	/**
	 * 세션값을 String으로 반환
	 * @param request
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static String getString(HttpServletRequest req, String key, String defaultValue)
	{
		HttpSession session = req.getSession();
	    String sessionValue = defaultValue ;
	    if(session == null)
	    {
	    	return sessionValue;
	    }

	    try{
	        if( session.getAttribute( key )== null ){
	            sessionValue = defaultValue ;
	        }else{

	            sessionValue = (String)session.getAttribute( key );
	        }
	    }catch(Exception e){
	    	sessionValue = defaultValue;
	    }
	    return sessionValue;
	}

	public static String getString(HttpServletRequest req, String key) {
		return SessionUtil.getString(req, key, "");
	}
	
	/**
	 * 세션값을 Object로 반환
	 * @param req
	 * @param key
	 * @return
	 */
	public static Object getObject(HttpServletRequest req, String key) {
		HttpSession session = req.getSession();
		return session.getAttribute(key); 
	}
	
	/**
	 * 세션값을 String으로 설정
	 * @param req
	 * @param key
	 * @param value
	 */
	public static void set(HttpServletRequest req, String key, Object value) {
		HttpSession session = req.getSession();
		session.setAttribute(key, value);
	}
	
	/**
	 * 세션값을 제거
	 * @param req
	 * @param key
	 */
	public static void remove(HttpServletRequest req, String key) {
		HttpSession session = req.getSession();
		session.removeAttribute(key);
	}
	
	/**
	 * 세션전체를 무효화
	 * @param req
	 */
	public static void removeAll(HttpServletRequest req) {
		HttpSession session = req.getSession();
		session.invalidate();
	}
}
